let util = require('../../utils/util');

let requestDomain = util.config('requestDomain');

let that;

Page({
    data: {
        dataType: '',
        infoId: '',
        infoName: '',
        oldInfoVal: '',
        infoVal: '',
        tip: '',
        maxLength: '',
		submitBtnDisabled: false
    },
	onLoad: function(params) {
        that = this;
        let {dataType, infoId, infoName, infoVal, tip, maxLength} = params;
        wx.setNavigationBarTitle({
            title: '修改' + infoName
        });
        that.setData({
            dataType: dataType,
            infoId: infoId,
            infoName: infoName,
            oldInfoVal: infoVal,
            infoVal: infoVal,
            tip: tip,
            maxLength: maxLength
        });
    },
	infoValInput: function(e) {
        if (e.detail.value == '') {
            that.setData({ submitBtnDisabled: true });
        }
        else {
            that.setData({ submitBtnDisabled: false });
        }
		that.setData({infoVal: e.detail.value});
	},
	submitUpdate: function() {
        let {dataType, infoId, infoName, infoVal} = that.data;
		wx.showModal({
			title: '确认修改？',
			content: '您将修改' + infoName + '，是否继续？',
			success: function(modal) {
				if(modal.confirm) {
					wx.showNavigationBarLoading();
					that.setData({submitBtnDisabled: true});
                    let requestUri = dataType == 'storeInfo' ? 'store/setStoreData' : 'role/setMyRoleInfo';
					wx.request({
                        url: requestDomain + requestUri,
						method: 'POST',
						data: {
							storeId: util.globalData('storeId'),
							_3rdSession: util.globalData('_3rdSession'),
							infoId: infoId,
							infoVal: infoVal
						},
						success: function (result) {
							wx.hideNavigationBarLoading();
							if(util.checkSuccess(result) && result.data && result.data.data) {
                                util.globalData('lastEditInfo', {
                                    infoId: infoId,
                                    infoVal: infoVal
                                });
                                wx.showToast({
                                    title: '修改成功',
                                    icon: 'success'
                                });
                                setTimeout(function() {
                                    wx.navigateBack();
                                }, 600);
                            }
                            else {
                                that.setData({ submitBtnDisabled: false });
                                util.showError('修改失败', '系统在处理你的修改时出现问题' + result.data.errMsg, result.data.errCode);
                            }
						},
						fail: function () {
							wx.hideNavigationBarLoading();
							that.setData({ submitBtnDisabled: false });
						}
					});
				}
			}
		});			
	}
});